﻿using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DC = DevExpress.ExpressApp.DC;

namespace aztall.Module.BusinessObjects
{
	public enum ConfigValueType { String, Integer, Date, Boolean };
	public enum ConfigGroup { Application, Internal, Promotion };


	[DefaultClassOptions, ImageName("ModelEditor_Settings"), NavigationItem("Administration")]
	[ModelDefault("AllowNew", "False")]
	public class Config : RootObject
	{
		protected ConfigGroup _ConfigGroup;
		protected string _ConfigName;
		protected string _Description;
		protected ConfigValueType _ConfigValueType;
		protected bool _IsInternal;

		public Config(Session session)
					: base(session)
		{
		}
		public override void AfterConstruction()
		{
			base.AfterConstruction();

		}
		[Index(1)]
		[DataSourceCriteria("IsInternal=true")]
		public ConfigGroup ConfigGroup
		{
			get { return _ConfigGroup; }
			set { SetPropertyValue(nameof(ConfigGroup), ref _ConfigGroup, value); }
		}
		[Index(2)]
		public string ConfigName
		{
			get { return _ConfigName; }
			set { SetPropertyValue(nameof(ConfigName), ref _ConfigName, value); }
		}
		[Index(3)]
		public string Description
		{
			get { return _Description; }
			set { SetPropertyValue(nameof(Description), ref _Description, value); }
		}
		[Index(5)]
		public ConfigValueType ValueType
		{
			get { return _ConfigValueType; }
			set { SetPropertyValue(nameof(ValueType), ref _ConfigValueType, value); }
		}
		[Association("Config-ConfigValue"), Aggregated(), DC.XafDisplayName("Config Values")]
		[ExplicitLoadingAttribute()]
		public XPCollection<ConfigValue> Values
		{
			get
			{
				return GetCollection<ConfigValue>(nameof(Values));
			}
		}

		public static XPCollection<Config> Find(Session session, ConfigGroup group, string name)
		{
			return new XPCollection<Config>(session, CriteriaOperator.Parse("ConfigGroup = ? and ConfigName = ?", group, name));
		}

	}
}
